package com.zhiyou100.controller;

import com.zhiyou100.entity.Message;
import com.zhiyou100.entity.ResponseEntity;
import com.zhiyou100.entity.UserInfo;
import com.zhiyou100.service.inter.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.hutool.core.util.ObjectUtil;


/**
 * 用户模块
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-10-06 11:41
 */
@Controller
@ResponseBody
public class UserInfoController {
    @Autowired
    private UserInfoService service;

    /**
     * 发送验证码
     *
     * @param phone   要发送的手机
     * @param request 域
     * @return 响应
     */
    @GetMapping("/user/sendCode/{phone}.action")
    public ResponseEntity<Object> sendCode(@PathVariable() String phone, HttpServletRequest request) {
        return service.sendMessage(phone, request);
    }

    /**
     * 短信验证码 登陆 接受 json message
     *
     * @param message message
     * @param request 域
     * @return 响应
     */
    @PostMapping("/user/login.action")
    public ResponseEntity<Object> loginByNumber(@RequestBody Message message, HttpServletRequest request) {
        return service.messageLogin(message.getPhoneNumber(), message.getCode(), request);
    }

    /**
     * 根据用户名和密码登陆
     *
     * @param name    用户名
     * @param pwd     密码
     * @param request 域
     * @return 响应
     */
    @PostMapping("/user/login/name/pwd.action")
    public ResponseEntity<Object> loginByNameAndPwd(@RequestParam("name") String name, @RequestParam("pwd") String pwd, HttpServletRequest request) {
        return service.login(name, pwd, request);
    }

    /**
     * 根据 id 获取一个 用户
     *
     * @param id 要获取用户的id
     * @return 响应
     */
    @GetMapping("/user/select/{id}.action")
    public ResponseEntity<Object> getOneById(@PathVariable String id) {
        final UserInfo userInfo = service.selectById(id);
        if (ObjectUtil.isNull(userInfo)) {
            return new ResponseEntity<>(500, "没有该id", null);
        } else {
            return new ResponseEntity<>(200, "没有该id", userInfo);
        }
    }

    /**
     * 更新 用户详情
     *
     * @param userInfo 要更新的用户
     * @return 响应
     */
    @PutMapping("/user/update.action")
    public ResponseEntity<Object> update(@RequestBody UserInfo userInfo) {
        if (ObjectUtil.isNull(userInfo)) {
            // 为空
            return new ResponseEntity<>(500, "user对象为 null", userInfo);
        }
        final UserInfo userInfo1 = service.selectById(userInfo.getUserId());
        if (ObjectUtil.isNull(userInfo1)) {
            // 没有 该用户
            return new ResponseEntity<>(500, "没有该用户", userInfo);
        }
        // 根据 id 更新
        service.updateById(userInfo);
        return new ResponseEntity<>(200, "更新成功！", userInfo);
    }

    /**
     * 退出
     *
     * @param session session
     * @return 响应
     */
    @GetMapping("/user/logout.action")
    public ResponseEntity<Object> logout(HttpSession session) {
        return service.logout(session);
    }
}
